package com.store.app.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.store.app.R;
import com.store.app.activity.CashierActivity;
import com.store.app.bean.GoodsListBean;
import com.store.app.utils.MoneyUtil;

import java.util.List;

/**
 * Created by Administrator on 2016/3/1.
 */
public class GoodsShowsAdapter extends BaseAdapter{
    private Context context;
    private List<GoodsListBean> listBeans ;
    public GoodsShowsAdapter(Context context,List<GoodsListBean> listBeans){
        this.context = context;
        this.listBeans = listBeans;
    }
    @Override
    public int getCount() {
        return listBeans.size();
    }

    @Override
    public Object getItem(int position) {
        return listBeans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

            convertView = LayoutInflater.from(context).inflate(R.layout.addmerchandise_list, null);

            TextView tvCount = (TextView) convertView.findViewById(R.id.quantity);
            TextView tvGoodsName = (TextView) convertView.findViewById(R.id.show_name);
            TextView tvPrice = (TextView) convertView.findViewById(R.id.show_amount);

            //删除
        convertView.findViewById(R.id.delete).setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    if(CashierActivity.goodsListBeanList.size()>0&&CashierActivity.goodsListBeanList.size()>position&& CashierActivity.goodsListBeanList.get(position)!=null){
                        CashierActivity.goodsListBeanList.remove(position);
                        CashierActivity.handler.sendEmptyMessage(2);
                    }
                }
            });
            //添加
        convertView.findViewById(R.id.increase).setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {

                    CashierActivity.goodsListBeanList.get(position).setCount
                            ( CashierActivity.goodsListBeanList.get(position).getCount()+1);
                    CashierActivity.handler.sendEmptyMessage(2);
                }
            });
            //减少
        convertView.findViewById(R.id.reduce).setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    if (CashierActivity.goodsListBeanList.get(position).getCount() <= 0) {
                        return;
                    }
                    CashierActivity.goodsListBeanList.get(position).setCount
                            (CashierActivity.goodsListBeanList.get(position).getCount() - 1);
                    CashierActivity.handler.sendEmptyMessage(2);
                }
            });
            if(!TextUtils.isEmpty(listBeans.get(position).getGoodsName())){
                tvGoodsName.setText(listBeans.get(position).getGoodsName());
            }else{
                tvGoodsName.setText("商品");
            }
            tvPrice.setText(MoneyUtil.formatMoney(listBeans.get(position).getPrice()));
            tvCount.setText(listBeans.get(position).getCount()+"");

        return convertView;
    }

//    public class ViewHolder {
//        private ImageView ivAdd;     //增加
//        private ImageView ivReduce;  //减少
//        private ImageView ivDelete;  //删除
//        private TextView tvGoodsName;//商品名称
//        private TextView tvPrice;    //商品单价
//        private TextView tvCount;    //商品数量
//        private LinearLayout llAdd,llReduce,llDelete;//增加、减少、删除
//    }
}
